'use strict';
(function() {
	var scirpts = document.scripts,
		scirpt = document.currentScript || scirpts[scirpts.length - 1],
		width = +scirpt.getAttribute('data-width') || 0,
		height = +scirpt.getAttribute('data-height') || 0,
		type = scirpt.getAttribute('data-type') || 'meet';
	scirpts = null;
	scirpt = null;
	if(!width && !height) {
		//不处理自适应
		return;
	}
	var html = document.documentElement,
		div = document.createElement('div'),
		style = document.createElement('style'),
		windowWidth = 0,
		windowHeight = 0,
		aspectRatio = 1,
		supportVW = false,
		delay,
		resize = function() {
			var cssText = '';
			if(supportVW) {
				if(width && height) {
					if(type === 'meet') {
						cssText = '@media screen and (min-aspect-ratio:' + width + '/' + height + '){html{font-size:' + (10000 / height) + 'vh;}}@media screen and (max-aspect-ratio:' + width + '/' + height + '){html{font-size:' + (10000 / width) + 'vw;}}';
					} else {
						cssText = '@media screen and (min-aspect-ratio:' + width + '/' + height + '){html{font-size:' + (10000 / width) + 'vw;}}@media screen and (max-aspect-ratio:' + width + '/' + height + '){html{font-size:' + (10000 / height) + 'vh;}}';
					}
				} else if(width) {
					cssText = 'html{font-size:' + (10000 / width) + 'vw;}';
				} else {
					cssText = 'html{font-size:' + (10000 / height) + 'vh;}';
				}
			} else {
				windowWidth = html.clientWidth || window.innerWidth || html.getBoundingClientRect().width;
				windowHeight = html.clientHeight || window.innerHeight || html.getBoundingClientRect().height;
				aspectRatio = windowWidth / windowHeight;
				if(width && height) {
					if(aspectRatio > width / height) {
						if(type === 'meet') {
							cssText = 'html{font-size:' + (windowHeight * 100 / height) + 'px;}';
						} else {
							cssText = 'html{font-size:' + (windowWidth * 100 / width) + 'px;}';
						}
					} else {
						if(type === 'meet') {
							cssText = 'html{font-size:' + (windowWidth * 100 / width) + 'px;}';
						} else {
							cssText = 'html{font-size:' + (windowHeight * 100 / height) + 'px;}';
						}
					}
				} else if(width) {
					cssText = 'html{font-size:' + (windowWidth * 100 / width) + 'px;}';
				} else {
					cssText = 'html{font-size:' + (windowHeight * 100 / height) + 'px;}';
				}
			}
			style.textContent = cssText;
		};
	try {
		div.style.width = '50vw';
	} catch(e) {

	}
	if(div.style.width === '50vw') {
		//支持vw
		supportVW = true;
	} else {
		//不支持vw
	}
	div = null;
	resize();
	window.addEventListener('resize', function() {
		if(window.requestAnimationFrame) {
			cancelAnimationFrame(delay);
			delay = requestAnimationFrame(resize);
		} else {
			clearTimeout(delay);
			delay = setTimeout(resize, 25);
		}
	});
	document.head.appendChild(style);
})();